/**
 * Class responsible for dealing with Battle Console Interface
 */
Console = new Class({
	
	Implements : [Options],
	
	options : {
	
	},
	
	private : {
		history : [],
		historyPointer : 1,
	},
	
	input : null,
	log : null,
	
	initialize : function(options){
		this.setOptions(options);
		this.initConsoleLog();
		this.initInputField();
	},
	
	initConsoleLog : function(){
		this.log = $('consoleLog');
	},

	initInputField : function(){
		this.input = $('consoleInput');
		var obj = this;
		this.input.addEvent('focus',function(){
			obj.input.setProperty('isFocused','1');
		});
		this.input.addEvent('blur',function(){
			obj.input.setProperty('isFocused','');
		});
		this.input.addEvent('keydown',function(e){
			switch(e.key){
				case "enter":	obj.submitCommand(e); break;
				case "up": 		this.set('value',obj.getLastCmd()); e.stop(); obj.moveCarat('end'); break;
				case "down": 	this.set('value',obj.getNextCmd()); e.stop(); obj.moveCarat('end'); break;
			}
		});
		this.input.focus();
	},
	
	submitCommand : function(e){
		if(e) e.stop();
		usrCmd = new GameCommand(this.input.get('value'));
		usrCmd.dispatch();
		if(this.input.get('value')) this.addToHistory(this.input.get('value'));
		this.input.set('value','');
	},
	
	/**
	 * Add to console history of commands and reset pointer to latest
	 */
	addToHistory : function(text){
		this.private.history[this.private.history.length] = text;
		this.private.historyPointer = this.private.history.length;
	},
	
	/**
	 * Returns last command from history
	 * @return string
	 */
	getLastCmd : function(){
		if(this.private.historyPointer >= 0){
			this.private.historyPointer--;
			return this.private.history[this.private.historyPointer];
		}else{
			return;
		}
	},

	/**
	 * Returns next command from history
	 * @return string
	 */
	getNextCmd : function(){
		if(this.private.historyPointer < this.private.history.length){
			this.private.historyPointer++;
			return this.private.history[this.private.historyPointer];
		}else{
			return;
		}
	},
	
	/** 
	 * Returns full command history
	 * @return array
	 */
	getHistory : function(asText){
		if(asText){
			var ret = "";
			for(var n=0;n<this.private.history.length;n++){
				ret += this.private.history[n]+"\n";
			}
			return ret;
		}else{
			return this.private.history;
		}
	},
	
	/**
	 * Log a command onto the battle console
	 * @return Element logItem
	 */
	logCommand : function(text){
		text = text.replace('\n','<br />'); // @todo finish
		var logItem = new LogItem(text);
		logItem.render();
		this.log.adopt(logItem);
		return logItem;
	},
	
	moveCarat : function(pos){
		if(pos=='end') pos = this.input.get('value').length;
		// Browser specific...
		if(this.input.createTextRange){ // IE 
	    	var range = this.input.createTextRange(); 
	        range.move("character", pos); 
	        range.select(); 
	    }else if(this.input.selectionStart){ 	// Gecko
	    	this.input.focus(); 
	    	this.input.setSelectionRange(pos, pos); 
	    }
	},
	
	/**
	 * Add chat msg to console
	 * @param Player player
	 * @param string msg
	 */
	addChatMsg : function(player,msg){
		var msg = "<strong>"+player.getName()+":</strong> "+msg;
		this.logCommand(msg);
	}
});

/**
 * Parse a game chat command
 * @return JSON
 */
Console.parseChatCmd = function(cmd){
	cmd += '';
	cmd = cmd.substr(5); // remove "chat "
	var parts = cmd.split(":");
	var player = (parts[0]).trim();
	switch(player.toLowerCase()){
		case 'all': player = 'All'; break;
		case 'team': player = 'Team'; break;
		default: player = Player.getPlayerByName(player); break;
	}
	return {'recip':player,'msg':(parts[1].trim())};
}